package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
)

var Order = &order{}

type order struct{}

// Create 创建
func (a *order) Create(ctx context.Context, m *entity.Order) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *order) Find(ctx context.Context, in *model.OrderInfoRequest) (*entity.Order, error) {
	e := &entity.Order{}

	q := GetDB(ctx).Model(&entity.Order{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

		count++
	}

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

		count++
	}

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

		count++
	}

	if in.Price != nil {

		q = q.Where("price = ?", in.Price)

		count++
	}

	if in.OrderNo != nil {

		q = q.Where("order_no like ?", in.OrderNo)

		count++
	}

	if in.StoreId != nil {

		q = q.Where("store_id = ?", in.StoreId)

		count++
	}

	if in.StoreName != nil {

		q = q.Where("store_name like ?", in.StoreName)

		count++
	}

	if in.AreaId != nil {

		q = q.Where("area_id = ?", in.AreaId)

		count++
	}

	if in.AreaName != nil {

		q = q.Where("area_name like ?", in.AreaName)

		count++
	}

	if in.Status != nil {

		q = q.Where("status = ?", in.Status)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *order) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.Order{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *order) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.Order{}, id).Error
}

// List 列表查询
func (a *order) List(ctx context.Context, in *model.OrderListRequest) (int, []*entity.Order, error) {
	var (
		q      = GetDB(ctx).Model(&entity.Order{})
		err    error
		total  int64
		orders []*entity.Order
	)

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

	}

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

	}

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

	}

	if in.Price != nil {

		q = q.Where("price = ?", in.Price)

	}

	if in.OrderNo != nil {

		q = q.Where("order_no like ?", in.OrderNo)

	}

	if in.StoreId != nil {

		q = q.Where("store_id = ?", in.StoreId)

	}

	if in.StoreName != nil {

		q = q.Where("store_name like ?", in.StoreName)

	}

	if in.AreaId != nil {

		q = q.Where("area_id = ?", in.AreaId)

	}

	if in.AreaName != nil {

		q = q.Where("area_name like ?", in.AreaName)

	}

	if in.Status != nil {

		q = q.Where("status = ?", in.Status)

	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	if in.StartTime != nil && in.EndTime != nil {
		q = q.Where("created_at between ? and ?", in.StartTime, in.EndTime)
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&orders).Error; err != nil {
		return 0, nil, err
	}
	return int(total), orders, nil
}

// ExecTransaction db事务执行
func (a *order) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
